package king.dominic.jlibrary.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

/**
 * Created by Administrator on 2017/10/13.
 * GpsGuideUtil
 */

public class GpsGuideUtil {
    private static final String GAODE_PACKAGE_NAME = "com.autonavi.minimap";
    private static final String BAIDU_PACKAGE_NAME = "com.baidu.BaiduMap";

    private GpsGuideUtil() {
    }

    public static void go(Context context, double latitude, double longitude) {
        if (PackageUtils.isAppAvailable(context, BAIDU_PACKAGE_NAME)) {
            openBaiduMapToGuide(context, latitude, longitude);
        } else if (PackageUtils.isAppAvailable(context, GAODE_PACKAGE_NAME)) {
            openGaodeMapToGuide(context, latitude, longitude);
        } else {
            openBrowserToGuide(context, latitude, longitude);
        }
    }

    private static void openBaiduMapToGuide(Context context, double latitude, double longitude) {
        Intent intent = new Intent();
        Uri uri = Uri.parse("baidumap://map/direction?" +
                "destination=" + latitude + "," + longitude + "&mode=driving");
        //将功能Scheme以URI的方式传入data
        intent.setData(uri);
        //启动该页面即可
        context.startActivity(intent);
    }

    private static void openGaodeMapToGuide(Context context, double latitude, double longitude) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        String url = "amapuri://route/plan/?dlat=" + latitude + "&dlon=" + longitude + "&dev=0&t=0";
        Uri uri = Uri.parse(url);
        //将功能Scheme以URI的方式传入data
        intent.setData(uri);
        //启动该页面即可
        context.startActivity(intent);
    }


    private static void openBrowserToGuide(Context context, double latitude, double longitude) {
        String url = "http://uri.amap.com/navigation?to=" + latitude + "," + longitude +
                "&mode=car&policy=1&src=mypage&coordinate=gaode&callnative=0";
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        context.startActivity(intent);
    }
}
